<?php if (!defined('FIT_PATH')) exit('No direct script access allowed');
/**
 * @copyright 2009 - 2011 Bincent.com. All Rights Reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @package	FitPHP
 * @subpackage library
 * 
 * @author Hongbin Hsu <hongbin.hsu@gmail.com> Initial.
 * @version $Id: library/hashmap.php 1.0 2010-09-18 16:52 hongbin.hsu $
 */
 
// ------------------------------------------------------------------------

class Library_Hashmap
{
    private $h_table;

    //----------------------------------------------------------
    /**
     * HashMap构造函数
     */
    public function __construct() {
    $this->h_table = array ();
    }

    //----------------------------------------------------------
    /**
     * 向HashMap中添加一个键值对
     *
     * @param $key 插入的键
     * @param $value 插入的值
     */
    public function put($key, $value) {
        if (!array_key_exists($key, $this->h_table)) {
            $this->h_table[$key] = $value;
            return null;
        } else {
            $temp_value = $this->h_table[$key];
            $this->h_table[$key] = $value;
            return $temp_value;
        }
    }

    //----------------------------------------------------------
    /**
     * 根据key获取对应的value
     *
     * @param $key
     */
    public function get($key) {
        if (array_key_exists($key, $this->h_table)){
            return $this->h_table[$key];
        }else{
            return null;
        }
    }

    //----------------------------------------------------------
    /**
     * 移除HashMap中所有键值对
     * 删除指定key的键值对
     *
     * @param $key 要移除键值对的key
    */
    public function remove($key) {
        $temp_table = array ();
        if (array_key_exists($key, $this->h_table)) {
            $temp_value = $this->h_table[$key];
            while ($cur_value = current($this->h_table)) {
                if (!(key($this->h_table) == $key))
                $temp_table[key($this->h_table)] = $cur_value;

                next($this->h_table);
            }
            $this->h_table = null;
            $this->h_table = $temp_table;
            return $temp_value;
        }else{
            return null;
        } 
    }

    //----------------------------------------------------------
    /**
     * 获取HashMap的所有键值
     *
     * @return 返回HashMap中key的集合,以数组形式返回
     */
    public function keys(){
        return array_keys($this->h_table);
    }

    //----------------------------------------------------------
    /**
     * 获取HashMap的所有value值
     */
    public function values(){
        return array_values($this->h_table);
    }

    //----------------------------------------------------------
    /**
     * 将一个HashMap的值全部put到当前HashMap中
     * @param $map
     */
    public function put_all($map){
        if(!$map->isEmpty()&& $map->size()>0){
            $keys = $map->keys();
            foreach($keys as $key){
                $this->put($key,$map->get($key));
            }
        }
    }

    //----------------------------------------------------------
    /**
     * 移除HashMap中所有元素
     */
    public function remove_all() {
        $this->h_table = null;
        $this->h_table = array();
    }

    //----------------------------------------------------------
    /**
     * HashMap中是否包含指定的值
     *
     * @param $value
     */
    public function contains_value($value) {
        while ($cur_value = current($this->h_table)) {
            if ($cur_value == $value) {
                return true;
            }
            next($this->h_table);
        }
        return false;
    }

    //----------------------------------------------------------
    /**
     * HashMap中是否包含指定的键key
     *
     * @param $key
     */
    public function contains_key($key) {
        if (array_key_exists($key, $this->h_table)) {
            return true;
        } else {
            return false;
        }
    }

    //----------------------------------------------------------
    /*
     * 获取HashMap中元素个数
     */
    public function size() {
        return count($this->h_table);
    }

    //----------------------------------------------------------
    /*
     * 判断HashMap是否为空
     */
    public function is_empty() {
        return (count($this->h_table) == 0);
    }

    //----------------------------------------------------------
    /**
     * 打印整个HashMap
     */
    public function to_string() {
        print_r($this->h_table);
    }
}
